package com.kang.delay;

import com.kang.utils.RabbitMQUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhangkangkang
 * @Description: 延迟队列，给不同的消息设置不同的过期时间
 * @date 2021/8/3 15:02
 */
public class Product {
    public static void main(String[] args) throws IOException, InterruptedException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        //设置死信交换机和队列
        channel.exchangeDeclare("dc", "direct");
        channel.queueDeclare("queuedelay", false, false, false, null);
        channel.queueBind("queuedelay", "dc", "rk");


        Map map = new HashMap();
        map.put("x-dead-letter-exchange", "dc");
        map.put("x-dead-letter-routing-key", "rk");
        channel.queueDeclare("mq3", false, false, false, map);

        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
        builder.expiration("5000");
        AMQP.BasicProperties build = builder.build();
        channel.basicPublish("", "mq3", build, ("hello" + System.currentTimeMillis()).getBytes());
        RabbitMQUtils.closeConnectionAndChanel(channel, connection);
    }
}
